package unittest;

import org.junit.Test;

import treeedit.BBFAlgorithm;
import treeedit.RandomTreeGenerator;
import treeedit.TedAlgorithm;
import treeedit.Tree;

public class Experiments {
	// @Test
	public void compareTED_Depth_Flat_Tree() {
		System.out.println("==============================================");
		int numberOfNodes = 10000;
		int distance = 2000;
		// =======TEST 10 TIMES============
		for (int i = 0; i < 10; i++) {
			long time = System.currentTimeMillis();
			Tree tree1 = RandomTreeGenerator.generateDeepOrFlatTree(numberOfNodes,
					"flat", distance);
			tree1.computeId(1);
			Tree tree1_clone = (Tree) tree1.clone();
			RandomTreeGenerator.randomizeTree(tree1_clone, 60);
			tree1_clone.computeId(1);

			Tree tree2 = RandomTreeGenerator.generateDeepOrFlatTree(numberOfNodes,
					"depth", distance);
			tree2.computeId(1);
			Tree tree2_clone = (Tree) tree2.clone();
			RandomTreeGenerator.randomizeTree(tree2_clone, 60);
			tree2_clone.computeId(1);

			// -----------Tree Edit Distance------------
			System.out
					.println(String
							.format("Distance = %s, run in %s ms, tree1: %s nodes, tree1_clone: %s nodes FLAT",
									TedAlgorithm.getDistance(tree1, tree1_clone),
									(System.currentTimeMillis() - time), tree1.getSize(),
									tree1_clone.getSize()));

			System.out
					.println(String
							.format("Distance = %s, run in %s ms, tree2: %s nodes, tree2_clone: %s nodes DEPTH",
									TedAlgorithm.getDistance(tree2, tree2_clone),
									(System.currentTimeMillis() - time), tree2.getSize(),
									tree2_clone.getSize()));

		}
		System.out.println("============Done=================");
	}

	@Test
	public void computeTEDWithFullRandomTree() {
		System.out.println("==============================================");
		long time = System.currentTimeMillis();
		System.out.println("Started TED...");
		// =======Initialize======
		int percent = 90;
		for (int i = 3; i <= 12; i++) {
			Tree root = RandomTreeGenerator.getFullRandomTree(i, 2, 2);
			Tree tree1 = (Tree) root.clone();
			Tree tree2 = (Tree) root.clone();
			RandomTreeGenerator.randomizeTree(tree1, percent);
			RandomTreeGenerator.randomizeTree(tree2, percent);
			tree1.computeId(1);
			tree2.computeId(1);
			// -----------Tree Edit Distance------------
			System.out
					.println(String
							.format("Distance = %s with %s layers, number of fanout = %s, run in %s ms, tree1: %s nodes, tree2: %s nodes",
									TedAlgorithm.getDistance(tree1, tree2), i, 2,
									(System.currentTimeMillis() - time), tree1.getSize(),
									tree2.getSize()));
		}

		System.out.println("============Done=================");
	}

	@Test
	public void computeTEDWithRandomTree() {
		System.out.println("==============================================");
		long time = System.currentTimeMillis();
		System.out.println("Started TED...");
		// =======Initialize======
		int percent = 30;
		int fanout = 4;
		for (int i = 2; i <= 7; i++) {
			Tree root = RandomTreeGenerator.getFullRandomTree(i, fanout, 2);
			Tree tree1 = (Tree) root.clone();
			Tree tree2 = (Tree) root.clone();
			RandomTreeGenerator.randomizeTree(tree1, percent);
			RandomTreeGenerator.randomizeTree(tree2, percent);
			tree1.computeId(1);
			tree2.computeId(1);
			// -----------Tree Edit Distance------------
			System.out
					.println(String
							.format("Distance = %s with %s layers, number of fanout = %s, run in %s ms, tree1: %s nodes, tree2: %s nodes",
									TedAlgorithm.getDistance(tree1, tree2), i, fanout,
									(System.currentTimeMillis() - time), tree1.getSize(),
									tree2.getSize()));
		}

		System.out.println("============Done=================");
	}

	@Test
	public void computeTEDWithBBFAndRandomTree() {
		System.out.println("==============================================");
		long time = System.currentTimeMillis();
		// =======Initialize======
		double threshold;
		for (double percent = 1; percent <= 3; percent = percent + 0.5) {
			System.out.println("Started TED + BBF... with percent = " + percent
					+ " of number of nodes of tree 1");
			for (int i = 3; i <= 8; i++) {
				Tree tree1 = RandomTreeGenerator.getFullRandomTree(i, 3, 2);
				tree1.computeId(1);
				threshold = percent * tree1.getSize();
				Tree tree2 = (Tree) tree1.clone();
				RandomTreeGenerator.randomizeTree(tree2, 50);
				tree2.computeId(1);
				// -----------Tree Edit Distance------------
				System.out
						.println(String
								.format("Distance = %s with %s layers, run in %s ms, tree1: %s nodes, tree2: %s nodes, with threshold= %s",
										BBFAlgorithm.getDistance(tree1, tree2, threshold),
										i, (System.currentTimeMillis() - time),
										tree1.getSize(), tree2.getSize(), threshold));
			}
		}

		System.out.println("============Done=================");
	}
}
